package com.lakeworks.listener;

import com.lakeworks.common.enums.OprProductType;
import com.lakeworks.common.enums.WorkType;
import com.lakeworks.common.enums.ticket.FlowType;
import com.lakeworks.common.enums.ticket.RouteKey;
import com.lakeworks.common.event.ReturnOrderEvent;
import com.lakeworks.common.exception.response.ResponseException;
import com.lakeworks.domain.OprPickup;
import com.lakeworks.domain.OprProduct;
import com.lakeworks.domain.TktProcess;
import com.lakeworks.event.TicketHoldingEvent;
import com.lakeworks.event.TicketWashModeEvent;
import com.lakeworks.common.domain.ticket.TktTicket;
import com.lakeworks.common.utils.StringUtils;
import com.lakeworks.event.TicketUpdateStatusEvent;
import com.lakeworks.mapper.TktProcessMapper;
import com.lakeworks.mapper.TktTicketMapper;
import com.lakeworks.service.IOprPickupService;
import com.lakeworks.service.IOprProductService;
import com.lakeworks.service.ITktTicketService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

@Component
public class TicketListener {

    private static final Logger log = LoggerFactory.getLogger(TicketListener.class);


    @Autowired
    private IOprProductService oprProductService;

    @Autowired
    private TktTicketMapper tktTicketMapper;

    @Autowired
    private ITktTicketService tktTicketService;

    @Autowired
    private IOprPickupService oprPickupService;

    @Autowired
    private TktProcessMapper tktProcessMapper;


    @EventListener
    public void holding(TicketHoldingEvent event) {
        log.info("执行工单变化事件，判断是否需要将订单暂停或者释放---event:[{}]",event);
        List<TktTicket> tickets = tktTicketMapper.selectTicketByOrderNoAndType(event.getOrderNo());
        if(tickets.isEmpty()){
            event.setCode(false);
        }else {
            event.setCode(true);
        }
        String code = oprProductService.holdingOrder(event.getOrderNo(), event.isCode());
        if (!code.equals("success")) {
            throw new ResponseException(code);
        }
    }

    @EventListener
    public void updateWashMode(TicketWashModeEvent event) {
        log.info("执行工单修改洗涤方式事件---event:[{}]",event);
        tktTicketService.updateWashMode(event.getBarcodeVos());
    }

    @EventListener
    public void updateProductStatus(TicketUpdateStatusEvent event){
        log.info("工单推动修改产品状态---event:[{}]",event);
        if(StringUtils.isNotBlank(event.getBarcode())
                && StringUtils.isNotBlank(event.getEventType())){
            if(event.getEventType().equals(RouteKey.UNDISCOVERED.name())){
                 if(event.getBarcode().contains(",")){
                     String[] barcodes = event.getBarcode().split(",");
                     Arrays.stream(barcodes).forEach(b->{
                         OprProduct product = oprProductService.selectProductByBarcodeAndStatus(OprProduct.builder().barcode(b).status(OprProductType.ATTACH.getValue()).build());
                         if(product != null){
                             product.setStatus(OprProductType.TOWRAP.getValue());
                             oprProductService.updateOprProduct(product);
                         }
                     });
                 }else {
                     OprProduct product = oprProductService.selectProductByBarcodeAndStatus(OprProduct.builder().barcode(event.getBarcode()).status(OprProductType.ATTACH.getValue()).build());
                     if(product != null){
                         product.setStatus(OprProductType.TOWRAP.getValue());
                         oprProductService.updateOprProduct(product);
                     }
                 }
            }
        }

    }



    @EventListener
    public void checkReturnOrder(ReturnOrderEvent event) {
        log.info("执行校验返厂单---event:[{}]",event);
        OprProduct product = oprProductService.queryByBarcode(event.getBarcode());
        if (product == null) {
            throw new ResponseException("产品不存在");
        }
        OprPickup pickup = oprPickupService.selectOprPickupById(product.getPickupId());
        if(pickup == null){
            throw new ResponseException("产品入厂单信息错误");
        }
        if(WorkType.RETURN.in(pickup.getFlag())){
            TktProcess tktProcess = tktProcessMapper.selectTktProcessByReturn(pickup.getOrderNo());
            if(tktProcess != null){
                List<TktProcess> processes = tktProcessMapper.selectTktProcessList(TktProcess.builder().ticketId(tktProcess.getTicketId()).build());
                long count = processes.stream().filter(p -> p.getFlag() == 2).count();
                if(count == 0){
                    throw new ResponseException("请联系ce查看返厂衣物是否满足顾客要求");
                }
            }
        }
    }

}
